<template>
  <div class="com-container">
    <div ref="chart_ref" class="com-chart" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      chartInstance: null, // echart实例
      allData: null, // 服务器返回的数据
      timerId: null, // 定时器标志
      currentIndex: 0 // 当前显示的数据
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      // console.log('主题切换了')
      this.chartInstance.dispose() // 销毁当前图表
      this.initChart() // 从新以最新主题初始化图表对象
      this.screenAdapter() // 完成屏幕适配
      this.updateChart() // 更新图表的展示
    }
  },
  // 在mounted后元素才被挂载上去才能通过ref拿到
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    // 在页面加载完成的时候，主动进行屏幕的适配
    this.screenAdapter()
  },
  destroyed() {
    clearInterval(this.timerId)
    // 在组件销毁的时候需要把监听器取消掉，防止内存的泄露
    window.removeEventListener('resize', this.screenAdapter)
  },
  methods: {
    // 初始化echartInstance对象
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.chart_ref, this.theme)
      // 对图表初始化配置的控制
      const initOption = {
        title: {
          text: '┃ 库存和销量分析',
          top: 20,
          left: 20
        }
      }
      this.chartInstance.setOption(initOption)
      // 对图表对象进行鼠标事件的监听
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    // 获取服务器数据
    getData() {
      const data = [{
        name: 'IPhone 11',
        sales: 2103,
        stock: 2310
      }, {
        name: '打底裤',
        sales: 2523,
        stock: 3310
      }, {
        name: 'HUAWEI P30',
        sales: 11747,
        stock: 13452
      }, {
        name: '打底毛衣宽松',
        sales: 3103,
        stock: 3722
      }, {
        name: '手机壳',
        sales: 7690,
        stock: 7822
      }, {
        name: '衬衫',
        sales: 2203,
        stock: 2310
      }, {
        name: '内裤',
        sales: 3103,
        stock: 5310
      }, {
        name: '秋裤',
        sales: 1103,
        stock: 2310
      }, {
        name: '情趣内衣',
        sales: 2103,
        stock: 2310
      }]
      this.allData = data
      this.updateChart()
      this.startInterval()
    },
    // 更新图表
    updateChart() {
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const colorArr = [
        ['#4ff778', '#0ba82c'],
        ['#e5dd45', '#e8811c'],
        ['#5052ee', '#ab6ee5'],
        ['#23e5e5', '#2e72bf'],
        ['#e8821c', '#e55445']
      ]
      // 处理图表需要的数据
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1) * 5
      const showData = this.allData.slice(start, end)
      const seriesArr = showData.map((item, index) => {
        return {
          type: 'pie',
          center: centerArr[index],
          hoverAnimation: false, // 关闭鼠标移入饼图时的动画效果
          labelLine: { // 隐藏指示线
            show: false
          },
          label: { // 标签文字设置
            position: 'center',
            color: colorArr[index][0]
          },
          data: [
            {
              name: item.name + '\n\n' + item.sales,
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  // 0%状态下的颜色值
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  },
                  // 100%状态下的颜色值
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            }
          ]
        }
      })
      const dataoption = {
        series: seriesArr
      }
      this.chartInstance.setOption(dataoption)
    },
    // 当浏览器大小发生变化的时候，会调用该方法来适配屏幕
    screenAdapter() {
      const titleFontSize = this.$refs.chart_ref.offsetWidth / 100 * 3.6 // 这个比例比较合适
      const innerRadius = titleFontSize * 2.8
      const outterRadius = innerRadius * 1.125
      // 和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      // 需要手动调用图表对象的resize方法才能产生效果
      this.chartInstance.resize()
    },
    // 启动定时器
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId)
      }

      this.timerId = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > 1) {
          this.currentIndex = 0
        }
        this.updateChart()
      }, 3000)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
